<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Detail extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('bbs_detail_model');
        $this->load->model('bbs_article_model');
        $this->load->model('bbs_user_model');
        $this->assign("module", "index");
    }

    function index($id,$comment_id) {
        if($comment_id){$comment_id = decrypt_comment_id($comment_id);}
        $this->info(decrypt_article_id($id),$comment_id);
        $this->assign('nickname', $this->bbs_userinfo['nickname']);
        $this->display('detail/index.html');
    }
    private function info($article_id,$comment_id){
        $where = array();
        $where['bbs_article.id'] = $article_id;
        $info = $this->bbs_detail_model->info($where);
        if(!$info){//如果帖子不存在
            $this->assign("error", "您查找的文章不存在或已被删除");
            $this->display('deleted/index.html');exit;
        }
        if(!$info['status'] || $info['status'] == 2){
            web_redirect("p/upub/".encrypt_article_id($article_id));
        }
        if($comment_id){
            $target_comment = $this->bbs_detail_model->get_article_comment(array("id"=>$comment_id,"article_id"=>$article_id,"is_show"=>1)); 
            if(!$target_comment){
                $this->assign("error", "您查找的回复不存在或已被删除");
                $this->display('deleted/index.html');exit;
            }
        }
        //重构信息-个人信息
        $info['create_time'] = date("Y-m-d H:i",$info['create_time']);
        $pid_cat = $this->bbs_article_model->get_article_category(array("id"=>$info['cat_id']));
        $info['cat_url'] = web_url("p/cat-".$info['cat_id']);
        $info['cat_name'] = $pid_cat['name'];
        $info['space_url'] = web_url("u/".  encrypt_user_id($info['authorid']));
        $info['detail_url'] = web_url("p/".  encrypt_article_id($article_id));
        $tags = array();
        if($info['tags_name']){
            $tags_arr = explode(",", $info['tags_name']);
            foreach ($tags_arr as $v){
                $v = explode("_", $v);
                $tags[$v[1]] = array("tid"=>$v[1],"tname"=>$v[0],"turl"=>  web_url("p/tag-".$v[1])); 
            }
        }
        $info['tags'] = $tags;
        $info['article_id'] = encrypt_article_id($article_id);
        $info['uuid'] = encrypt_user_id($info['authorid']);
        
        
        //判断是否加关注
        $is_fan = $this->bbs_userinfo ? ($this->bbs_user_model->get_fan_status(array("user_id"=>$info['authorid'],"followuid"=>$this->bbs_userinfo['user_id'],"is_del"=>1)) ? 1 : 0) : 0;
        //判断是否点赞
        $is_like = $this->bbs_userinfo ? ($this->bbs_detail_model->get_article_like_status(array("user_id"=>$this->bbs_userinfo['user_id'],"article_id"=>$article_id)) ? 1 : 0) : 0;
        //判断是否收藏
        $is_favorite = $this->bbs_userinfo ? ($this->bbs_detail_model->get_article_favorite_status(array("user_id"=>$this->bbs_userinfo['user_id'],"article_id"=>$article_id,"is_del"=>1)) ? 1 : 0) : 0;
        
        $info['is_fan'] = $is_fan;
        $info['is_like'] = $is_like;
        $info['is_favorite'] = $is_favorite;
        $info['share_url'] = share_url("p/".$info['article_id']);
        $this->assign('info', $info);
        
        //打赏记录
        $reward = $this->db->select("r.user_id,r.target_id,r.create_time,u.head_img,u.nickname")
                ->from("bbs_reward r")
                ->join("users_info u","u.user_id = r.user_id","left")
                ->where(array("r.target_id"=>$article_id,"r.target_type"=>'article'))->order_by("r.id desc")->group_by("r.user_id")->get()->result_array();
        foreach ($reward as $key=>$val){
            $reward[$key]['space_url'] = web_url("u/".encrypt_user_id($val['user_id']));
        }
        $this->assign('reward', $reward);
        
        $where = array();
        $where['bbs_article_comment.article_id'] = $article_id;
        $where['bbs_article_comment.is_show'] = 1;
        //获取评论总条数
        $comment_totalCount = $this->bbs_detail_model->get_article_comment_count($where);
        $this->assign("comment_totalCount",$comment_totalCount);
        $this->assign("comment_id",$target_comment ? encrypt_comment_id($comment_id) : "");
        $this->assign('nickname', $this->bbs_userinfo['nickname']);
        
        $this->right();
        $this->related_article($article_id,$tags);
        
        $this->assign('site_title', $info['title']);
    }
    
    //文章详情右侧信息
    private function right(){
        //热门标签
        $hot_tags = $this->db->select("id,name")->from("bbs_article_tags")->where(array("is_hot"=>1))->order_by("sort desc")->get()->result_array();
        foreach ($hot_tags as $key=>$val){
            $hot_tags[$key]['url'] = web_url("p/tag-".$val['id']);
        }
        
        $this->assign("hot_tags", $hot_tags);
        
        //非热门标签
        $common_tags = $this->db->select("id,name")->from("bbs_article_tags")->where("is_hot != 1")->order_by("sort desc")->get()->result_array();
        foreach ($common_tags as $key=>$val){
            $common_tags[$key]['url'] = web_url("p/tag-".$val['id']);
        }
        $this->assign("common_tags", $common_tags);
    }
    
    //问答页面相关问题
    private function related_article($article_id,$tags){
        $like = "(";
        $count = count($tags);
        $index = 0;
        foreach ($tags as $key => $tag){
            $index += 1;
            if($count == $index){
                $like .= " (tags_name like '%$tag[tname]%')";
            }else{
                $like .= " (tags_name like '%$tag[tname]%') or ";
            }
            
        }
        $like .= ")";
        
        $relate_article = $this->db->query("select id,title,commentnum from tan_bbs_article where id not in($article_id) and $like order by sort desc limit 5")->result_array();
        foreach ($relate_article as $key =>$val){
            $relate_article[$key]['title'] = mb_strcut($val['title'], 0, 50, "utf8")."...";
            $relate_article[$key]['detail_url'] = web_url("p/".encrypt_article_id($val['id']));
        }
        $this->assign('relate_article', $relate_article);
    }
    
    //文章预览
    function view($id){
        
        $item = $this->db->select("tan_bbs_article_drafts.*,tan_users_info.head_img,tan_users_info.nickname")->from("bbs_article_drafts")->join("users_info","tan_users_info.user_id = tan_bbs_article_drafts.authorid","left")->where(array("tan_bbs_article_drafts.id"=>$id))->get()->row_array();
        if(!$item){//如果帖子不存在
            $this->assign("error", "您查找的文章不存在或已被删除");
            $this->display('deleted/index.html');exit;
        }
        $tags = $item['tags_name'];
        $tags_arr = array();
        if($tags){
            $tags = explode(",", $tags);
            foreach ($tags as $key=>$val){
                $tag = explode("_", $val);
                $tags_arr[] = array("id"=>$tag[1],"name"=>$tag[0],"url"=>web_url("p/tag-".$tag[1]));
            }
        }
        //获取板块信息
        $cat = $this->bbs_article_model->get_article_category(array("id"=>$item['cat_id']));
        $item['cat_name'] = $cat['name'];
        $item['tags'] = $tags_arr;
        $item['space_url'] = web_url("u/".encrypt_user_id($item['authorid']));
        $item['create_time'] = date("Y-m-d H:i:s",$item['create_time']);
        
        $this->right();
        
        $this->assign("item", $item);
        $this->display("detail/view.html");
    }
    
    //审核中的页面
    function upub($article_id){
        $article_id = decrypt_article_id($article_id);
        $where = array();
        $where['bbs_article.id'] = $article_id;
        $item = $this->bbs_detail_model->info($where);
        if(!$item){//如果帖子不存在
            $this->assign("error", "您查找的文章不存在或已被删除");
            $this->display('deleted/index.html');exit;
        }
        if($item['status'] == 1){
            web_redirect("p/".encrypt_article_id($article_id));
        }
        $uinfo = $this->db->query("select nickname,head_img from tan_users_info where user_id = $item[authorid] order by id desc limit 1")->row_array();
        $item['head_img'] = $uinfo['head_img'];
        $item['nickname'] = $uinfo['nickname'];
        $tags = $item['tags_name'];
        $tags_arr = array();
        if($tags){
            $tags = explode(",", $tags);
            foreach ($tags as $key=>$val){
                $tag = explode("_", $val);
                $tags_arr[] = array("id"=>$tag[1],"name"=>$tag[0],"url"=>web_url("p/tag-".$tag[1]));
            }
        }
        $item['tags'] = $tags_arr;
        $item['cat_url'] = web_url("p/cat-".$item['cat_id']);
        $item['create_time'] = $this->set_time_two($item['create_time']);
        
        $this->right();
        
        $this->assign("item", $item);
        $this->display("detail/upub.html");
    }
}
